﻿using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Configuration;
using System.Web;
using System.Web.Security;


/// <summary>
///PhotoControl 的说明
///缩略图类
/// </summary>
public class PhotoControl
{
    public PhotoControl()
    {

    }


    /// <summary>
    /// 图片高清晰缩略图
    /// </summary>
    /// <param name="originalImagePath">源图路经</param>
    /// <param name="thumbnailPath">新图路经</param>
    /// <param name="targetSize">缩放大小</param>
    public static void MakeThumbnail(System.Web.HttpPostedFile originalImagePath, string thumbnailPath, int targetSize)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalImagePath.InputStream, true);

        Size newSize = new Size();
        if (originalImage.Width > targetSize || originalImage.Height > targetSize)
        {
            if (originalImage.Height > originalImage.Width)
            {
                newSize.Width = (int)(originalImage.Width * ((float)targetSize / (float)originalImage.Height));
                newSize.Height = targetSize;
            }
            else
            {
                newSize.Width = targetSize;
                newSize.Height = (int)(originalImage.Height * ((float)targetSize / (float)originalImage.Width));
            }
        }
        else
        {
            newSize.Width = originalImage.Width;
            newSize.Height = originalImage.Height;
        }



        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(newSize.Width, newSize.Height);

        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //清空画布并以透明背景色填充
        g.Clear(System.Drawing.Color.Transparent);

        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, newSize.Width, newSize.Height),
            new System.Drawing.Rectangle(0, 0, originalImage.Width, originalImage.Height),
            System.Drawing.GraphicsUnit.Pixel);

        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

    public static void MakeThumbnail(System.Web.HttpPostedFile originalImagePath, string thumbnailPath, int width, int height)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalImagePath.InputStream, true);

        Size newSize = new Size();

        float size = (float)originalImage.Width / (float)originalImage.Height;//上传图片的比例
        float nsize = (float)width / (float)height;//设定的比例
        if (originalImage.Width > width || originalImage.Height > height)
        {
            if (size < nsize)
            {
                newSize.Width = (int)(originalImage.Width * ((float)height / (float)originalImage.Height));
                newSize.Height = height;
            }
            else
            {
                newSize.Width = width;
                newSize.Height = (int)(originalImage.Height * ((float)width / (float)originalImage.Width));
            }
        }
        else
        {
            newSize.Width = originalImage.Width;
            newSize.Height = originalImage.Height;
        }

        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(newSize.Width, newSize.Height);

        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //清空画布并以透明背景色填充
        g.Clear(System.Drawing.Color.Transparent);

        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, newSize.Width, newSize.Height),
            new System.Drawing.Rectangle(0, 0, originalImage.Width, originalImage.Height),
            System.Drawing.GraphicsUnit.Pixel);

        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }
}